from django.contrib.auth.backends import ModelBackend
from user.models import User
import re

def jwt_response_payload_handler(token, user=None, request=None):

    return {
        'token':token,
        'username':user.username,
        'user_id':user.id,
    }


# 重写用户名验证
class MobileNameView(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        # 判断username的数据类型＇
        try:
            # 假如查询不到的话，抛出异常
            if re.match(r'1[3-9]\d{9}$',username):
                #　这是个对象，这条信息符合的话，返回的一串数据，这个对象有这个多个属性
                user = User.objects.get(mobile=username)

            else:
                user = User.objects.get(username=username)
        except:
            user = None

        if user is not None and user.check_password(password):
            return user
        print(user)
        return user



